package zhishu.iceyyy.com.bxq_2500.activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;


import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jzvd.Jzvd;
import zhishu.iceyyy.com.bxq_2500.CustomJzvd.MyJzvdStd;
import zhishu.iceyyy.com.bxq_2500.R;
import zhishu.iceyyy.com.bxq_2500.adapter.HorizontalListViewAdapter;
import zhishu.iceyyy.com.bxq_2500.adapter.HorizontalListViewAdapter2;
import zhishu.iceyyy.com.bxq_2500.api.API;
import zhishu.iceyyy.com.bxq_2500.api.Okhttp;
import zhishu.iceyyy.com.bxq_2500.bean.MoviceDetail;
import zhishu.iceyyy.com.bxq_2500.bean.addCollection;
import zhishu.iceyyy.com.bxq_2500.dialog.MenuDialog;
import zhishu.iceyyy.com.bxq_2500.myadapter.Grid2ViewAdapter;
import zhishu.iceyyy.com.bxq_2500.myadapter.ListViewAdapter;
import zhishu.iceyyy.com.bxq_2500.ui.HorizontalListView;
import zhishu.iceyyy.com.bxq_2500.ui.MyGridView;
import zhishu.iceyyy.com.bxq_2500.utils.JSONUtils;

public class Work_Activity extends AppCompatActivity {
    MoviceDetail.DataBean.PlayMoviceBean playMovice;


    @BindView(R.id.im_fenxiang)
    ImageView imFenxiang;
    @BindView(R.id.im_shoucang)
    ImageView imShoucang;

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.textView3)
    TextView textView3;
    @BindView(R.id.tv_bieming)
    TextView tvBieming;
    @BindView(R.id.tv_gengxinshijian)
    TextView tvGengxinshijian;
    @BindView(R.id.tv_pingfen)
    TextView tvPingfen;
    @BindView(R.id.tv_tuijian)
    TextView tvTuijian;
    @BindView(R.id.gv_images)
    MyGridView gvImages;
    @BindView(R.id.activity_seetting)
    LinearLayout activitySeetting;
    Grid2ViewAdapter grid2ViewAdapter;
    List<MoviceDetail.DataBean.MovicesBean.MoviceBean> movice;
    @BindView(R.id.xiangqing_re)
    RelativeLayout xiangqingRe;
    @BindView(R.id.save_address)
    ScrollView scrollView;

    HorizontalListViewAdapter horizontalListViewAdapter;
    HorizontalListViewAdapter2 horizontalListViewAdapter2;
    @BindView(R.id.hlv)
    HorizontalListView hlv;

    @BindView(R.id.banner1)
    Banner banner1;


    @BindView(R.id.hlv2)
    HorizontalListView hlv2;
    @BindView(R.id.jz_video)
    MyJzvdStd myJzvdStd;
    Context mContext;
    List<String> ccList = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work);



        mContext = this;

        String value = getIntent().getStringExtra("value");
        ButterKnife.bind(this);
        myJzvdStd.SAVE_PROGRESS=true;
        scrollView.smoothScrollTo(0, 20);
        scrollView.setFocusable(true);
        horizontalListViewAdapter = new HorizontalListViewAdapter();
        horizontalListViewAdapter2 = new HorizontalListViewAdapter2();
        hlv.setAdapter(horizontalListViewAdapter);
        hlv2.setAdapter(horizontalListViewAdapter2);
        hlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                horizontalListViewAdapter.setChecked(position);//传入现在选择的position 现在用到了
                horizontalListViewAdapter.notifyDataSetInvalidated();//重绘
                String s = values.get(position);
                player(s);
            }
        });
        hlv2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                horizontalListViewAdapter2.setChecked(position);//传入现在选择的position 现在用到了
                horizontalListViewAdapter2.notifyDataSetInvalidated();//重绘
                String s = xianlu.get(position);
                String s1 = xianluvalue.get(position);
                getshuju3(s1);
//                Toast.makeText(Work_Activity.this, "" + s, Toast.LENGTH_SHORT).show();
            }
        });
        grid2ViewAdapter = new Grid2ViewAdapter(this);
        gvImages.setAdapter(grid2ViewAdapter);
        gvImages.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                MoviceDetail.DataBean.MovicesBean.MoviceBean moviceBean = movice.get(i);
                startActivity(new Intent(Work_Activity.this, Work_Activity.class).putExtra("value", "" + moviceBean.getVod_id()));
                finish();
            }
        });

        API.moviceDetail("0", value, new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                Log.e("111111", "xx" + st);
                scrollView.smoothScrollTo(0, 20);
                scrollView.setFocusable(true);
                MoviceDetail MoviceDetail = JSONUtils.parseJSON(st, MoviceDetail.class);
                if (MoviceDetail.getCode().equals("200")) {
                    MoviceDetail.DataBean data = MoviceDetail.getData();
                    playMovice = data.getPlayMovice();
                    String vod_tag = playMovice.getVod_year();
                    String score = "" + playMovice.getVod_score();
                    tvBieming.setText(vod_tag == null ? "别名:" + "暂无" : "别名:" + vod_tag);
                    tvGengxinshijian.setText(vod_tag == null ? "更新:" + "暂无" : "更新:" + vod_tag);
                    tvPingfen.setText(score == null ? "评分:暂无" : "评分:" + score);
                    tvTitle.setText("" + playMovice.getVod_name() == null ? "暂无" : playMovice.getVod_name());
                    List<MoviceDetail.DataBean.MovicesBean> movices = data.getMovices();
                    movice = movices.get(0).getMovice();
                    grid2ViewAdapter.clear();
                    grid2ViewAdapter.addItem(movice);

                    String vod_play_from = playMovice.getVod_play_from();
                    Log.e("ZXCZXC", playMovice.getVod_play_url());

                    getshuju2(playMovice.getVod_play_url());
                    List<zhishu.iceyyy.com.bxq_2500.bean.MoviceDetail.DataBean.HeaderAdvBean> headerAdv = data.getHeaderAdv();
                    if (headerAdv != null && headerAdv.size() > 0) {
                        ccList.clear();
                        for (int i = 0; i < headerAdv.size(); i++) {
                            ccList.add(headerAdv.get(i).getMac_adv_show_url());
                        }
                        banner1.setVisibility(View.VISIBLE);
                        startBanner(banner1, ccList);
                    } else {
                        banner1.setVisibility(View.GONE);
                    }

                } else {
                    tvBieming.setText("別名:" + "暂无");
                    tvGengxinshijian.setText("更新:" + "暂无");
                    tvPingfen.setText("评分:" + "暂无");
                }

            }

            @Override
            public void onFalia(int code, String errst) {
                Log.e("ZXCZXC", "xx" + errst);
                tvBieming.setText("別名:" + "暂无");
                tvGengxinshijian.setText("更新:" + "暂无");
                tvPingfen.setText("评分:" + "暂无");
            }
        });
    }


    /**
     * 显示dialog
     */
    private void showMenuDialog() {
        if (mMenuDialog != null) {
            mMenuDialog = null;
        }
        Date date = new Date();
        date.setTime(Long.valueOf(playMovice.getVod_time()));
        String format = new SimpleDateFormat().format(date);
        mMenuDialog = new MenuDialog<>(this, "MenuDialog", null, true,
                false,
                playMovice.getVod_name(), playMovice.getVod_year(), playMovice.getVod_director(), playMovice.getVod_actor(),
                playMovice.getVod_area(), "" + format, playMovice.getVod_year(), "" + playMovice.getVod_score(),
                playMovice.getVod_blurb());
        mMenuDialog.show();
    }

    private MenuDialog<String> mMenuDialog;

    /**
     * 销毁dialog
     */
    private void dismissMenuDialog() {
        if (mMenuDialog != null && mMenuDialog.isShowing()) {
            mMenuDialog.dismiss();
            mMenuDialog = null;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
            showMenuDialog();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @OnClick({R.id.im_fenxiang, R.id.im_shoucang, R.id.xiangqing_re, R.id.img_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.im_fenxiang:
                Toast.makeText(Work_Activity.this, "分享按钮", Toast.LENGTH_SHORT).show();
                break;
            case R.id.im_shoucang:
                shoucang();
                break;
            case R.id.xiangqing_re:
                showMenuDialog();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

//        myJzvdStd.get
    }


    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    public void shoucang() {
        if (playMovice == null) {
            return;

        }
        API.addCollection(playMovice.getVod_id(), new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                Log.e("zzz", "xx" + st);
                addCollection addCollection = JSONUtils.parseJSON(st, addCollection.class);
                if (addCollection.getCode().equals("200")) {
                    Toast.makeText(Work_Activity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(Work_Activity.this, "收藏失败", Toast.LENGTH_SHORT).show();
                }


            }

            @Override
            public void onFalia(int code, String errst) {
                Log.e("zzz", "xx" + errst);
                Toast.makeText(Work_Activity.this, "网络错误！", Toast.LENGTH_SHORT).show();
            }
        });
    }


    public void player(String name) {
        Jzvd.releaseAllVideos();

        myJzvdStd.setUp(name
                , " ");
//        myJzvdStd.seekToInAdvance=2000;


        myJzvdStd.startVideo();

    }

    List<String> name = new ArrayList<String>();
    List<String> values = new ArrayList<String>();

    public void getshuju(String vod_play_url) {
        String[] c = vod_play_url.split("#");
        for (int i = 0; i < c.length; i++) {
//            Log.e("zzz", "xx:" + c[i]);
            String[] split = c[i].split("\\$");

            name.add(split[0]);
            values.add(split[1]);

        }
        horizontalListViewAdapter.addItem(name);
        player(values.get(0));
    }

    public void getshuju3(String vod_play_url) {
        name.clear();
        values.clear();
        String[] c = vod_play_url.split("#");
        for (int i = 0; i < c.length; i++) {
//            Log.e("zzz", "xx:" + c[i]);
            String[] split = c[i].split("\\$");

            name.add(split[0]);
            values.add(split[1]);

        }
        horizontalListViewAdapter.clear();
        horizontalListViewAdapter.addItem(name);

    }

    List<String> xianlu = new ArrayList<String>();
    List<String> xianluvalue = new ArrayList<String>();

    public void getshuju2(String vod_play_url) {
        xianlu.clear();
        xianluvalue.clear();
        String[] split = vod_play_url.split("\\$\\$\\$");
        for (int i = 0; i < split.length; i++) {
//            Log.e("zzz", "xx:" + c[i]);


            xianlu.add("线路" + (i + 1));
            xianluvalue.add(split[i]);

        }
        getshuju(xianluvalue.get(0));
        horizontalListViewAdapter2.addItem(xianlu);
        horizontalListViewAdapter.setChecked(0);//传入现在选择的position 现在用到了
        horizontalListViewAdapter.notifyDataSetInvalidated();//重绘
        horizontalListViewAdapter2.setChecked(0);//传入现在选择的position 现在用到了
        horizontalListViewAdapter2.notifyDataSetInvalidated();//重绘
    }

    private void startBanner(Banner banner, List<String> imageUrl) {
        //设置banner样式(显示圆形指示器)
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置指示器位置（指示器居右）
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(imageUrl);
        //设置banner动画效果
//        banner.setBannerAnimation(Transformer.DepthPage);
        //设置标题集合（当banner样式有显示title时）
//        banner.setBannerTitles(titles);
        //设置自动轮播，默认为true
//        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(5000);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }

    public class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            //Glide 加载图片简单用法
            Glide.with(context).load(path).into(imageView);


        }


    }

}
